package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureDesignProcedureDto;
import com.guigu.erp.mapper.ManufactureDesignProcedureMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.ManufactureDesignProcedure;
import com.guigu.erp.pojo.ManufactureDesignProcedureDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.ManufactureDesignProcedureDetailsService;
import com.guigu.erp.service.ManufactureDesignProcedureService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManufactureDesignProcedureServiceImpl extends ServiceImpl<ManufactureDesignProcedureMapper, ManufactureDesignProcedure>
    implements ManufactureDesignProcedureService {

    @Autowired
    private ManufactureDesignProcedureMapper manufactureDesignProcedureMapper;

    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private DesignFileService designFileService;

    @Override
    public String getMaxDesignId() {
        ManufactureDesignProcedure designModule = manufactureDesignProcedureMapper.queryMaxModule();
        if (designModule == null){
            return null;
        }
        String designId = designModule.getDesignId();
        return designId;
    }

    @Override
    @Transactional
    public boolean addManufactureProcess(ManufactureDesignProcedureDto dto) {
         //1.添加工序设计表
          this.save(dto);
         //2.添加工序明细设计表
        //获取明细表的数据
        List<ManufactureDesignProcedureDetails> process = dto.getProcess();
        for(int i=0;i<process.size();i++){
            ManufactureDesignProcedureDetails details = process.get(i);
            //设置每条工序明细的id为null
            details.setId(null);
            // 获取物料设计单的id
            Integer parentId = dto.getId();
            // 设置父级序号
            details.setParentId(parentId);
            // 设置工序序号
            details.setDetailsNumber(i+1);
            // 设置单位
            if(details.getAmountUnit()==null){
                details.setAmountUnit("小时");
            }
            //设置工序物料标志
            details.setDesignModuleTag("D002-0");
            //设置工序物料变更标志
            details.setDesignModuleChangeTag("D003-0");
            // 插入工序明细
            manufactureDesignProcedureDetailsService.save(details);
        }
        //3.修改产品档案表工序设计标志
        String productId = dto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        designFile.setDesignProcedureTag("G001-1");
        boolean flag = designFileService.updateById(designFile);
        return flag;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> getManufactureList(DesignModuleVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        //查询条件判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("FIRST_KIND_ID",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("SECOND_KIND_ID",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("THIRD_KIND_ID",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_reviewStatus())){
                queryWrapper.eq("CHECK_TAG",vo.getSearch_reviewStatus());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("REGISTER_TIME",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("REGISTER_TIME",vo.getSearch_registerTime2());
            }
        }
        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(queryWrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> getAllManufactureList(DesignModuleVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        //查询条件判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("FIRST_KIND_ID",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("SECOND_KIND_ID",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("THIRD_KIND_ID",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_reviewStatus())){
                queryWrapper.eq("DESIGN_MODULE_TAG",vo.getSearch_reviewStatus());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("REGISTER_TIME",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("REGISTER_TIME",vo.getSearch_registerTime2());
            }
        }
        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(queryWrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }
}
